import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:top_foundation/component/window/window.dart';
import 'package:top_foundation/utils/theme/theme.dart';

import 'helper/route/router_config.dart';

import 'helper/utils/methodchannelmanager.dart';

import 'package:flutter_localizations/flutter_localizations.dart';

import 'helper/utils/util.dart';

void main() async {
  // UMApmMan().register(
  //   callback: () async {
  //     // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
  //     //     overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
  //     // MethodChannelManager.initChannel();

  //     // TopConfig.imgPlaceholder = 'appIcon.png'.imgPath;
  //     // TopConfig.imgDefaultAvater = 'avater.png'.imgPathMe;

  //     // TopConfig.imgEmpty = 'imgEmpty.png'.imgPathCommon;
  //     // ITheme.text = ITextTheme();

  //     // BuglyMan().register(() => runApp(const MyApp()));
  //   },
  // );
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
      overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
  ]);
  MethodChannelManager.initChannel();

  TopConfig.imgPlaceholder = 'appIcon.png'.imgPath;
  TopConfig.imgDefaultAvater = 'avater.png'.imgPathMe;

  TopConfig.imgEmpty = 'imgEmpty.png'.imgPathCommon;
  ITheme.text = ITextTheme();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  // MyApp([this._navigatorObserver]);
  const MyApp({super.key});
  // final NavigatorObserver? _navigatorObserver;
  @override
  Widget build(BuildContext context) {
    return ScreenAdapterInit(
      designSize: const Size(393, 844),
      builder: (context) => GetMaterialApp(
        theme: ITheme.theme,
        initialRoute: RouterPath.splash,
        onGenerateRoute: MyRouterConfig.generateRoute,
        // navigatorObservers: [UmengAppAnalysis(), ],
        getPages: RouterPages.getPages,
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate, //iOS
        ],
        supportedLocales: const [
          Locale('zh', 'CN'),
          Locale('en', 'US'),
        ],
        builder: (context, child) {
          child = IWindow.init(context, child);
          child = Hud.init(context, child);

          child = MediaQuery(
              //设置文字大小不随系统设置改 变
              data: MediaQuery.of(context)
                  .copyWith(textScaler: TextScaler.noScaling),

              //全局设置键盘收缩
              child: GestureDetector(
                onTap: () {
                  FocusScopeNode focus = FocusScope.of(context);
                  if (!focus.hasPrimaryFocus && focus.focusedChild != null) {
                    FocusManager.instance.primaryFocus?.unfocus();
                  }
                },
                child: child,
              ));
          return child;
        },
      ),
    );
  }
}
